<!DOCTYPE html><html lang="en"><head><title>BillingAgreementCreateAndExecute.aspx</title></head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0"><meta name="groc-relative-root" content=""><meta name="groc-document-path" content="BillingAgreementCreateAndExecute.aspx"><meta name="groc-project-path" content="BillingAgreementCreateAndExecute.aspx.cs"><link rel="stylesheet" type="text/css" media="all" href="assets/style.css"><script type="text/javascript" src="assets/behavior.js"></script><body><div id="meta"><div class="file-path">BillingAgreementCreateAndExecute.aspx.cs</div></div><div id="document"><div class="segment"><div class="code"><div class="wrapper">﻿<span class="hljs-keyword">using</span> System;
<span class="hljs-keyword">using</span> System.Web;
<span class="hljs-keyword">using</span> PayPal.Api;
<span class="hljs-keyword">using</span> PayPal.Sample.Utilities;

namespace PayPal.Sample
{
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">partial</span> <span class="hljs-keyword">class</span> BillingAgreementCreateAndExecute : BaseSamplePage
    {
        <span class="hljs-keyword">protected</span> <span class="hljs-keyword">override</span> <span class="hljs-keyword">void</span> <span class="hljs-title">RunSample</span>()
        {</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h3 id="api-context">Api Context</h3>
<p>Pass in a <code>APIContext</code> object to authenticate 
the call and to send a unique request id 
(that ensures idempotency). The SDK generates
a request id if you do not pass one explicitly. 
See <a href="/Source/Configuration.html">Configuration.cs</a> to know more about APIContext.</p></div></div><div class="code"><div class="wrapper">            <span class="hljs-keyword">var</span> apiContext = Configuration.GetAPIContext();

            <span class="hljs-keyword">string</span> token = Request.Params[<span class="hljs-string">"token"</span>];
            <span class="hljs-keyword">if</span> (<span class="hljs-keyword">string</span>.IsNullOrEmpty(token))
            {
                <span class="hljs-keyword">this</span>.CreateBillingAgreement(apiContext);
            }
            <span class="hljs-keyword">else</span>
            {</div></div></div><div class="segment"><div class="code folded"><div class="wrapper marker"><span class="c1">                // Ignore workflow code segment</span></div><div class="wrapper">                <span class="hljs-comment">// Ignore workflow code segment</span>
                <span class="hljs-preprocessor">#<span class="hljs-keyword">region</span> Track Workflow</span>
                <span class="hljs-keyword">this</span>.flow = Session[<span class="hljs-string">"flow-"</span> + Request.Params[<span class="hljs-string">"guid"</span>]] <span class="hljs-keyword">as</span> RequestFlow;
                <span class="hljs-keyword">this</span>.RegisterSampleRequestFlow();
                <span class="hljs-keyword">this</span>.flow.RecordApproval(<span class="hljs-string">"Agreement approved successfully."</span>);
                <span class="hljs-preprocessor">#<span class="hljs-keyword">endregion</span></span>

                <span class="hljs-keyword">this</span>.ExecuteBillingAgreement(apiContext, token);
            }
        }

        <span class="hljs-comment"><span class="hljs-xmlDocTag">///</span> <span class="hljs-xmlDocTag">&lt;summary&gt;</span></span>
        <span class="hljs-comment"><span class="hljs-xmlDocTag">///</span> </span>
        <span class="hljs-comment"><span class="hljs-xmlDocTag">///</span> <span class="hljs-xmlDocTag">&lt;/summary&gt;</span></span>
        <span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">CreateBillingAgreement</span>(APIContext apiContext)
        {</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><p>Before we can setup the billing agreement, we must first create a
billing plan that includes a redirect URL back to this test server.</p></div></div><div class="code"><div class="wrapper">            <span class="hljs-keyword">var</span> plan = BillingPlanCreate.CreatePlanObject(HttpContext.Current);
            <span class="hljs-keyword">var</span> guid = Convert.ToString((<span class="hljs-keyword">new</span> Random()).Next(<span class="hljs-number">100000</span>));
            plan.merchant_preferences.return_url = Request.Url.ToString() + <span class="hljs-string">"?guid="</span> + guid;</div></div></div><div class="segment"><div class="code folded"><div class="wrapper marker"><span class="c1">            // Ignore workflow code segment</span></div><div class="wrapper">            <span class="hljs-comment">// Ignore workflow code segment</span>
            <span class="hljs-preprocessor">#<span class="hljs-keyword">region</span> Track Workflow</span>
            <span class="hljs-keyword">this</span>.flow.AddNewRequest(<span class="hljs-string">"Create new billing plan"</span>, plan);
            <span class="hljs-preprocessor">#<span class="hljs-keyword">endregion</span></span>

            <span class="hljs-keyword">var</span> createdPlan = plan.Create(apiContext);</div></div></div><div class="segment"><div class="code folded"><div class="wrapper marker"><span class="c1">            // Ignore workflow code segment</span></div><div class="wrapper">            <span class="hljs-comment">// Ignore workflow code segment</span>
            <span class="hljs-preprocessor">#<span class="hljs-keyword">region</span> Track Workflow</span>
            <span class="hljs-keyword">this</span>.flow.RecordResponse(createdPlan);
            <span class="hljs-preprocessor">#<span class="hljs-keyword">endregion</span></span></div></div></div><div class="segment"><div class="comments "><div class="wrapper"><p>Activate the plan</p></div></div><div class="code"><div class="wrapper">            <span class="hljs-keyword">var</span> patchRequest = <span class="hljs-keyword">new</span> PatchRequest()
            {
                <span class="hljs-keyword">new</span> Patch()
                {
                    op = <span class="hljs-string">"replace"</span>,
                    path = <span class="hljs-string">"/"</span>,
                    <span class="hljs-keyword">value</span> = <span class="hljs-keyword">new</span> Plan() { state = <span class="hljs-string">"ACTIVE"</span> }
                }
            };</div></div></div><div class="segment"><div class="code folded"><div class="wrapper marker"><span class="c1">            // Ignore workflow code segment</span></div><div class="wrapper">            <span class="hljs-comment">// Ignore workflow code segment</span>
            <span class="hljs-preprocessor">#<span class="hljs-keyword">region</span> Track Workflow</span>
            <span class="hljs-keyword">this</span>.flow.AddNewRequest(<span class="hljs-string">"Update the plan"</span>, patchRequest);
            <span class="hljs-preprocessor">#<span class="hljs-keyword">endregion</span></span>

            createdPlan.Update(apiContext, patchRequest);</div></div></div><div class="segment"><div class="code folded"><div class="wrapper marker"><span class="c1">            // Ignore workflow code segment</span></div><div class="wrapper">            <span class="hljs-comment">// Ignore workflow code segment</span>
            <span class="hljs-preprocessor">#<span class="hljs-keyword">region</span> Track Workflow</span>
            <span class="hljs-keyword">this</span>.flow.RecordActionSuccess(<span class="hljs-string">"Plan updated successfully"</span>);
            <span class="hljs-preprocessor">#<span class="hljs-keyword">endregion</span></span></div></div></div><div class="segment"><div class="comments "><div class="wrapper"><p>With the plan created and activated, we can now create the billing agreement.</p></div></div><div class="code"><div class="wrapper">            <span class="hljs-keyword">var</span> payer = <span class="hljs-keyword">new</span> Payer() { payment_method = <span class="hljs-string">"paypal"</span> };
            <span class="hljs-keyword">var</span> shippingAddress = <span class="hljs-keyword">new</span> ShippingAddress()
            {
                line1 = <span class="hljs-string">"111 First Street"</span>,
                city = <span class="hljs-string">"Saratoga"</span>,
                state = <span class="hljs-string">"CA"</span>,
                postal_code = <span class="hljs-string">"95070"</span>,
                country_code = <span class="hljs-string">"US"</span>
            };

            <span class="hljs-keyword">var</span> agreement = <span class="hljs-keyword">new</span> Agreement()
            {
                name = <span class="hljs-string">"T-Shirt of the Month Club"</span>,
                description = <span class="hljs-string">"Agreement for T-Shirt of the Month Club"</span>,
                start_date = <span class="hljs-string">"2015-02-19T00:37:04Z"</span>,
                payer = payer,
                plan = <span class="hljs-keyword">new</span> Plan() { id = createdPlan.id },
                shipping_address = shippingAddress
            };</div></div></div><div class="segment"><div class="code folded"><div class="wrapper marker"><span class="c1">            // Ignore workflow code segment</span></div><div class="wrapper">            <span class="hljs-comment">// Ignore workflow code segment</span>
            <span class="hljs-preprocessor">#<span class="hljs-keyword">region</span> Track Workflow</span>
            <span class="hljs-keyword">this</span>.flow.AddNewRequest(<span class="hljs-string">"Create billing agreement"</span>, agreement);
            <span class="hljs-preprocessor">#<span class="hljs-keyword">endregion</span></span></div></div></div><div class="segment"><div class="comments "><div class="wrapper"><p>Create the billing agreement.</p></div></div><div class="code"><div class="wrapper">            <span class="hljs-keyword">var</span> createdAgreement = agreement.Create(apiContext);</div></div></div><div class="segment"><div class="code folded"><div class="wrapper marker"><span class="c1">            // Ignore workflow code segment</span></div><div class="wrapper">            <span class="hljs-comment">// Ignore workflow code segment</span>
            <span class="hljs-preprocessor">#<span class="hljs-keyword">region</span> Track Workflow</span>
            <span class="hljs-keyword">this</span>.flow.RecordResponse(createdAgreement);
            <span class="hljs-preprocessor">#<span class="hljs-keyword">endregion</span></span></div></div></div><div class="segment"><div class="comments "><div class="wrapper"><p>Get the redirect URL to allow the user to be redirected to PayPal to accept the agreement.</p></div></div><div class="code"><div class="wrapper">            <span class="hljs-keyword">var</span> links = createdAgreement.links.GetEnumerator();

            <span class="hljs-keyword">while</span> (links.MoveNext())
            {
                <span class="hljs-keyword">var</span> link = links.Current;
                <span class="hljs-keyword">if</span> (link.rel.ToLower().Trim().Equals(<span class="hljs-string">"approval_url"</span>))
                {
                    <span class="hljs-keyword">this</span>.flow.RecordRedirectUrl(<span class="hljs-string">"Redirect to PayPal to approve billing agreement..."</span>, link.href);
                }
            }
            Session.Add(<span class="hljs-string">"flow-"</span> + guid, <span class="hljs-keyword">this</span>.flow);
            Session.Add(guid, createdAgreement.token);
        }

        <span class="hljs-comment"><span class="hljs-xmlDocTag">///</span> <span class="hljs-xmlDocTag">&lt;summary&gt;</span></span>
        <span class="hljs-comment"><span class="hljs-xmlDocTag">///</span> </span>
        <span class="hljs-comment"><span class="hljs-xmlDocTag">///</span> <span class="hljs-xmlDocTag">&lt;/summary&gt;</span></span>
        <span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">ExecuteBillingAgreement</span>(APIContext apiContext, <span class="hljs-keyword">string</span> token)
        {
            <span class="hljs-keyword">var</span> agreement = <span class="hljs-keyword">new</span> Agreement() { token = token };</div></div></div><div class="segment"><div class="code folded"><div class="wrapper marker"><span class="c1">            // Ignore workflow code segment</span></div><div class="wrapper">            <span class="hljs-comment">// Ignore workflow code segment</span>
            <span class="hljs-preprocessor">#<span class="hljs-keyword">region</span> Track Workflow</span>
            <span class="hljs-keyword">this</span>.flow.AddNewRequest(<span class="hljs-string">"Execute billing agreement"</span>, description: <span class="hljs-keyword">string</span>.Format(<span class="hljs-string">"URI: v1/payments/billing-agreements/{0}/agreement-execute"</span>, agreement.token));
            <span class="hljs-preprocessor">#<span class="hljs-keyword">endregion</span></span>
            
            <span class="hljs-keyword">var</span> executedAgreement = agreement.Execute(apiContext);</div></div></div><div class="segment"><div class="code folded"><div class="wrapper marker"><span class="c1">            // Ignore workflow code segment</span></div><div class="wrapper">            <span class="hljs-comment">// Ignore workflow code segment</span>
            <span class="hljs-preprocessor">#<span class="hljs-keyword">region</span> Track Workflow</span>
            <span class="hljs-keyword">this</span>.flow.RecordResponse(executedAgreement);
            <span class="hljs-preprocessor">#<span class="hljs-keyword">endregion</span></span>
        }
    }
}</div></div></div></div></body></html>